<template>
  <page-header-wrapper :title="false">
    <a-card title='资源回收站'>
      <div class="table-page-search-wrapper">
        <a-form-model :model="queryParam" layout="inline">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户的ID">
                <a-input v-model="queryParam['filter[customer_id]']"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务分类">
                <a-select v-model="queryParam['first_type']" :default-value="0">
                  <a-select-option v-for="(option,index) in options['firstType']" :key="index" :value="index">
                    {{option}}
                  </a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="业务名称">
                  <a-input v-model="queryParam['filter[b_name]']"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="入库时间">
                  <a-range-picker @change="onTime" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="搜索的IP">
                  <a-input v-model="queryParam['filter[ip]']"/>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="按搜索词">
                  <a-input v-model="queryParam['filter[search_keywords]']"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="搜索引擎">
                  <a-input v-model="queryParam['filter[search_engine]']"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="首次页面">
                  <a-input v-model="queryParam['filter[first_url]']"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="领取顾问">
                  <a-input v-model="queryParam['filter[receiver_info]']"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="联系电话">
                  <a-input v-model="queryParam['filter[phone]']"/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
                <span class='table-page-search-submitButtons' :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                  <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
                  <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                  <a @click="toggleAdvanced" style="margin-left: 8px">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <a-button :disabled="selectedRowKeys.length == 0" type="danger" @click="onDelect(selectedRowKeys)">批量恢复</a-button>
      <p></p>
      <s-table
        bordered
        ref='table'
        :columns="customerColumns"
        :data="customerData"
        :row-key="(record) => record.customer_id"
        :scroll="{ x: 1200 }"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <template slot='tm_name' slot-scope='text,record'>
          {{text}}{{record.pt_name}}
        </template>
        <template slot='first_url' slot-scope='text'>
          <a :href='text' target='_blank'>{{text}}</a>
        </template>
        <template slot='action' slot-scope='text,record'>
          <a-button type='primary' size='small' @click="onDelect(record.customer_id)">恢复</a-button>
        </template>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import col from '@/columns/customer'
import { recycleList, delRecycle } from '@/api/customer'
export default {
  components: {
    STable
  },
  name: 'Recycle',
  data () {
    return {
      customerColumns: col.recyclelist,
      customerData: parameters => {
        return recycleList(Object.assign(parameters, this.queryParam)).then(res => {
          this.options['firstType'] = res.result.first_type_opts
          return res.result
        })
      },
      queryParam: {},
      loading: false,
      advanced: true,
      options: {},
      selectedRowKeys: []
    }
  },
  methods: {
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    onDelect (ids) {
      const app = this
      app.$confirm({
        title: '确定恢复数据吗',
        onOk: () => {
          const parameter = {}
          parameter['ids'] = ids
          delRecycle(parameter).then(res => {
            if (res.status === 0) {
              app.$refs.table.refresh(true)
            } else {
              this.$warning({
                title: res.msg
              })
            }
          })
        }
      })
    },
    resetForm () {
      const app = this
      app.queryParam = {}
      app.$refs.table.refresh(true)
    },
    onTime (date, dateString) {
      for (let i = 0; i < dateString.length; i++) {
        if (i === 0) {
          this.queryParam['filter[created_at_start]'] = dateString[i]
        }
        if (i === 1) {
          this.queryParam['filter[created_at_end]'] = dateString[i]
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
